package cn.bytecollege.service;

import cn.bytecollege.mapper.UserAddressMapper;
import cn.bytecollege.mapper.UserDetailMapper;
import cn.bytecollege.model.UserAddress;
import cn.bytecollege.model.UserDetail;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PersonalService {
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private UserDetailMapper userDetailMapper;

    public UserAddress getUserAddressByUserId(Integer userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);
        List<UserAddress> userAddresses = userAddressMapper.selectByMap(map);
        if (userAddresses.size()==0) {
            return null;
        }
        return userAddresses.get(0);
    }

    public UserDetail getUserDetailByUserId(Integer userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);
        List<UserDetail> userDetails = userDetailMapper.selectByMap(map);

        if (userDetails.size()==0){
            return null;
        }
        return userDetails.get(0);
    }

    public Integer modUserDetailByUserId(UserDetail userDetail) {
        QueryWrapper<UserDetail> user_id = new QueryWrapper<UserDetail>().eq("user_id", userDetail.getUserId());
        return userDetailMapper.update(userDetail,user_id);
    }
}
